<?php


namespace app\admin\controller;
use app\admin\model\Goods as GoodsModel;                //商品
use app\admin\model\GoodsAttr as GoodsAttrModel;        //商品属性
use app\admin\model\GoodsType as GoodsTypeModel;        //商品分类
use app\admin\model\GoodsBrand as GoodsBrandModel;      //商品品牌
use app\admin\model\Order as OrderModel;                //订单
use app\admin\model\OrderDetail as OrderDetailModel;    //订单详情
use app\admin\model\Receive as ReceiveModel;            //收货人
use app\admin\model\Send as SendModel;                  //物流方式
use app\admin\model\Pay as PayModel;                    //支付方式

use think\Request;      //请求
use think\Paginator;    //分页



class Order extends Base
{
    //显示订单
    public function lists()
    {
        //遍历订单表
        $order = OrderModel::where(['order_del'=>0,'is_pay'=>1])->paginate(4);
        $orderPage = $order->render();

        //遍历收件人表
        $receive = ReceiveModel::where(['receive_del'=>0])->select();
        //dump($receive);

        //遍历配送方式
        $send = SendModel::where(['send_del'=>0])->select();

        //传值,渲染
        $webSite = WEB_SITE;
        $staticAdmin = STATIC_ADMIN;

        $this->assign([
            'webSite' => $webSite,
            'staticAdmin' => $staticAdmin,
            'order'     => $order,
            'orderPage' => $orderPage,
            'receive' => $receive,
            'send' => $send,

        ]);
        return $this->fetch();
    }


    //订单回收站
    public function receive()
    {
        //遍历订单表
        $order = OrderModel::where(['order_del'=>1,'is_pay'=>1])->paginate(4);
        $orderPage = $order->render();

        //遍历收件人表
        $receive = ReceiveModel::where(['receive_del'=>0])->select();
        //dump($receive);

        //遍历配送方式
        $send = SendModel::where(['send_del'=>0])->select();

        //传值,渲染
        $webSite = WEB_SITE;
        $staticAdmin = STATIC_ADMIN;

        $this->assign([
            'webSite' => $webSite,
            'staticAdmin' => $staticAdmin,
            'order'     => $order,
            'orderPage' => $orderPage,
            'receive' => $receive,
            'send' => $send,

        ]);
        return $this->fetch();
    }


    //回收,恢复
    public function flag()
    {
        $data = [];
        $tip  = '';
        $post = Request::instance()->param();
        $ordId = $post['ordId'];
        //dump($post);

        //当前时间datetime格式
        $currTime = date('Y-m-d H:i:s',time());
        $data['update_time'] = $currTime;

        //回收,伪删除
        if (Request::instance()->param('receive') > -1) {

            $data['order_del'] = $post['receive'];
            $tip = '删除';
        } else if (Request::instance()->param('disDel') > -1) {

            //恢复
            $data['order_del'] = $post['disDel'];
            $tip = '恢复';
        }

        //修改数据库
        $res = OrderModel::where(['order_id'=>$ordId])->update($data);
        //dump($data);
        //dump($res);

        //是否修改成功
        if ($res) {

            echo json_encode(['state'=>1, 'tip'=>"{$tip}成功"]);  //拼接提示信息
        } else {

            echo json_encode(['state'=>2, 'tip'=>"{$tip}失败"]);  //拼接提示信息
        }


    }

    //订单详情
    public function detail()
    {
        $ordId = Request::instance()->param('oid');
        //dump($ordId);

        //查询 (所有订单)
        $order = OrderModel::get(['order_id'=>$ordId]);
        $shouldPay = $order->should_pay;
        $lastPid = $order->last_paid;

        //外键
        $receiveId = $order->receive_id;
        $sendId = $order->send_id;
        $detailId = $order->detail_id;
        //dump($order);
        //dump($receiveId);
        //dump($detailId);

        //查询收货人信息
        $receive = ReceiveModel::get(['receive_id'=>$receiveId]);
        //dump($receive->receive_name);

        //订单详情
        $detail = OrderDetailModel::where('detail_id','in',"$detailId")->select();  //查询范围 in
        //dump($detail);

        //取出订单中 所有商品id
        foreach ($detail as $deta) {
            //dump($deta);
            $goodsId[] = $deta->goods_id;

            //若每一类商品对应一条商品数据行
           /* $attrId[] = $deta->attr_id;
            $brandId[] = $deta->brand_id;
            $typeId[]  = $deta->type_id;*/
        }
        //dump($goodsId);

        $total = count($goodsId);
        //dump($total);

        //遍历出, 每一件商品的详情信息
        for ($i = 0 ; $i < $total; $i++) {

            //获取商品id
            $gid = $goodsId[$i];

            //保存订单中所有商品信息,(编号和图片)
            $good[$i] = GoodsModel::get(['goods_id'=>$gid]);

            //依次取出商品的属性,品牌,分类,并保存在数组中
            $goods = GoodsModel::get(['goods_id'=>$gid]);
            //若每一件商品对应一条商品数据行
            $attrId[$i] = $goods->attr_id;
            $brandId[$i] = $goods->brand_id;
            $typeId[$i]  = $goods->type_id;
        }
        // dump($good);
        // dump($goodsCard);
        // dump($attrId);

        //属性
        for ($k = 0; $k < count($attrId); $k++) {

            $aId = $attrId[$k];
            $tId = $typeId[$k];
            $bId = $brandId[$k];
            $brand[] = GoodsBrandModel::get(['brand_id'=>$bId]);
            $attr[] = GoodsAttrModel::get(['attr_id'=>$aId]);
            $type[] = GoodsTypeModel::get(['type_id'=>$tId]);
        }
        // dump($attr);
        // dump($brand);
        // dump($type);

        //物流信息
        $send = SendModel::get(['send_id'=>$sendId]);
        //dump($send);

        //传值,渲染
        $webSite = WEB_SITE;
        $staticAdmin = STATIC_ADMIN;

        $this->assign([
            'webSite' => $webSite,
            'staticAdmin' => $staticAdmin,
            'order'   => $order,
            'receive' => $receive,
            'detail'  => $detail,
            'good'    => $good,
            'send'    => $send,
            'brand'   => $brand,
            'attr'    => $attr,
            'type'    => $type,

        ]);
        return $this->fetch();
    }

    //订单状态修改
    public function orderStatus()
    {
        $ordId = Request::instance()->param('oid');

        if (Request::instance()->param('pay')) {

            $data['is_pay'] = Request::instance()->param('pay');

        } else if (Request::instance()->param('status')) {

            $data['order_status'] = Request::instance()->param('status');
        }

        //修改数据库
        $order = OrderModel::where(['order_id'=>$ordId])->update($data);

        if ($order) {

            echo json_encode(['state'=>1,'tip'=>'修改成功']);
        } else {

            echo json_encode(['state'=>2,'tip'=>'修改失败']);
        }

    }


}